#include "ADCJ.h"
#include "main.h"
#include "ADS1247.h"
#include "DS18B20.h"
#include "TC.h"

#define S24_S32(f) ((f & 0x800000) ? (f | 0xff000000) : (f)) 

float  fADJS_RTD;



struct FILTER_REG FR[11];

// 冒泡滤波计算：总数8个数据，去掉两个最高值，去掉两个最低值，剩下4个计算平均值
//  data: 	 	 需要滤波的数据，16位有符号数
// *Buffer： 	 数据对应的缓冲区
// 数据滤波算法：
// 1. 如果数据变化较大，不使用滤波直接输出原数，使数据变化实时
// 2. 如果数据变化缓慢，可以使用滤波，
//    第一：如果数据变化缓慢次数大于10次，使用冒泡滤波计算
//    第二：如果数据变化缓慢次数小于10次，使用上次通道输出和本次计算结果的平均数输出
short FILTER(unsigned char ch,short siData){
    unsigned char i,j;
	short filter_buf[FILTER_N];		//滤波计算缓冲区
	short filter_temp;				//临时寄存器
	int   filter_sum;				//计算和
	
	FR[ch].buf[0] = siData;

    // 如果数据变化较大，不使用滤波直接输出原数，使数据变化实时
	if(((siData-AI[ch])>FR[i].DValue)||((AI[ch]-siData)>FR[i].DValue)){
		filter_temp = siData;
		FR[ch].JS = 0;
	}else{
		//数据变化较小，使用数据滤波，检测数据变缓次数，来确定使用什么滤波
		if(FR[ch].JS > 10){
			//如果数据变化缓慢次数大于10次，使用冒泡滤波计算
			for(i=0;i<FILTER_N;i++) filter_buf[i] = FR[ch].buf[i];	//把需要滤波的数据复制到缓冲区
			//采样值从小到大排列（冒泡法）
			for(j = 0; j < FILTER_N - 1; j++){
			  for(i = 0; i < FILTER_N - 1 - j; i++){
				if(filter_buf[i] > filter_buf[i + 1]){
				  filter_temp = filter_buf[i];
				  filter_buf[i] = filter_buf[i + 1];
				  filter_buf[i + 1] = filter_temp;
				}
			  }
			}
			//去除最大最小极值后求平均
			filter_sum = 0;
			for(i = ReMove; i < FILTER_N - ReMove; i++)  filter_sum += filter_buf[i];
			filter_temp =  filter_sum / (FILTER_N - ReMove_Sun);  
		}else{
			//如果数据变化缓慢次数小于10次，使用上次通道输出和本次计算结果的平均数输出
			FR[ch].JS++;
			filter_temp = (s16)(((s32)siData + AI[ch])/2);
		}
	}
	
	//数据缓冲区数据整体上移一格，准备接受新数据
	for(i=(FILTER_N-1);i>0;i--) FR[ch].buf[i] = FR[ch].buf[i-1];
	return filter_temp;
}

void MV_V(unsigned char ucCH)
{   
    s16 siTemp;
    s32 slTemp;
	
    slTemp = S24_S32(CJ.slREG[ucCH]);
	fADJS_RTD = (float)slTemp;
	
    switch(AIMD.ucAITypeB[ucCH])    /*转为电压*/
    {   
        case 0: fADJS_RTD = ((fADJS_RTD * SB.REF_15mV) / 8388608)*2;break;
        case 1: fADJS_RTD = ((fADJS_RTD * SB.REF_50mV) / 8388608);break;
        case 2: fADJS_RTD = ((fADJS_RTD * SB.REF_100mV) / 8388608);break;
        case 3: fADJS_RTD = ((fADJS_RTD * SB.REF_1V) / 8388608);break;
    }
    
    if(ucCH == 0)  SB.REFJZ[0] = (s16)fADJS_RTD;        /*校准参考数值*/
    
    if(fADJS_RTD > 32767)    fADJS_RTD = 32767;         /*超量程处理*/
    else if(fADJS_RTD < -32768)   fADJS_RTD = -32768;   /*超量程处理*/
    siTemp = (s16)fADJS_RTD;
    
	AI[ucCH] = FILTER(ucCH,siTemp);	/*滤波计算*/
	
    bKI[ucCH] = 1;   /*通道在线*/
}

void MV_mA(u8 ucCH)
{
    s16 siTemp;
	int slTemp;
	
	slTemp = S24_S32(CJ.slREG[ucCH]);
	fADJS_RTD = (float)slTemp;
    
    fADJS_RTD = (fADJS_RTD * SB.REF_20mA[ucCH]) / 8388608; /*转为电流*/
    
    SB.REFJZ[ucCH] = (s16)fADJS_RTD;    /*校准数据*/
    
    if(fADJS_RTD > 32767)    siTemp = 32767;
    else if(fADJS_RTD < -32768)   siTemp = -32768;
    else siTemp = (s16)fADJS_RTD;

    if(((AIMD.ucAITypeA[ucCH] == 4)&&((AIMD.ucAITypeB[ucCH] == 1)||(AIMD.ucAITypeB[ucCH] == 3))) &&(siTemp<6000)){
        AI[ucCH] =  6000;
		FR[ucCH].JS = 0;
        if(siTemp < 3750)       bKI[ucCH] = 0;   //通道断线 //小于2.5mA
        else if(siTemp > 5250)  bKI[ucCH] = 1;   //通道在线//大于3.5mA
    }else{
        bKI[ucCH] = 1;   //通道在线
		AI[ucCH] = FILTER(ucCH,siTemp);	//滤波计算
    }  
}

s16 MVTC(u8 TYPE,float fMV)
{
    s16 temp;
    switch(TYPE)
    {
        case 0: temp = J_VtoT(fMV/16); break; //J:16X
        case 1: temp = K_VtoT(fMV/32); break; //K:32X
        case 2: temp = T_VtoT(fMV/32); break; //T:64X
        case 3: temp = E_VtoT(fMV/16); break; //E:16X
        case 4: temp = R_VtoT(fMV/64); break; //R:64X
        case 5: temp = S_VtoT(fMV/64); break; //S:64X
        case 6: temp = B_VtoT(fMV/64); break; //B:64X
        case 7: temp = N_VtoT(fMV/32); break; //N:32X
    }
    return temp;
}

s16 MVTC1(u8 TYPE,float fMV)
{
    s16 temp;
    switch(TYPE)
    {
        case 0: temp = J_VtoT(fMV/16 + J_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //J:16X
        case 1: temp = K_VtoT(fMV/32 + K_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //K:32X
        case 2: temp = T_VtoT(fMV/32 + T_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //T:64X
        case 3: temp = E_VtoT(fMV/16 + E_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //E:16X
        case 4: temp = R_VtoT(fMV/64 + R_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //R:64X
        case 5: temp = S_VtoT(fMV/64 + S_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //S:64X
        case 6: temp = B_VtoT(fMV/64 + B_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //B:64X
        case 7: temp = N_VtoT(fMV/32 + N_TtoV((float)(SB.SBPT+SB.SBPTZ)/10)); break; //N:32X
    }
    return temp;
}

void MV_TC(u8 ucCH)
{  
    s16 siTemp,siTempPD;
	s32 slTemp;
	
    slTemp = S24_S32(CJ.slREG[ucCH]);
	fADJS_RTD = (float)slTemp;
	
    /*将转换值转换为电压*/
    fADJS_RTD = ((fADJS_RTD * SB.REF_TC) / 8388608);
    
    siTemp = MVTC(AIMD.ucAITypeB[ucCH],fADJS_RTD);  /*mV电压转换为温度*/
    siTempPD = MVTC1(AIMD.ucAITypeB[ucCH],fADJS_RTD);  /*mV电压转换为温度*/
    
    if((siTemp>29000)|| (siTemp < -9000))
    {
        AI[ucCH] = siTemp;
        bKI[ucCH] = 0;   /*通道断线*/
		FR[ucCH].JS = 0;
    }else{
        bKI[ucCH] = 1;   /*通道在线*/
        if(AIMD.ucQAITypeC == 0) siTemp = siTempPD;
		
		AI[ucCH] = FILTER(ucCH,siTemp);	/*滤波计算*/
    }
}

